package com.project.service.impl;

import com.project.bean.CarBean;
import com.project.bean.ManBean;
import com.project.mapper.IManMapper;
import com.project.service.IManService;

import java.util.Arrays;
import java.util.List;

public class ManServiceImpl extends BaseService implements IManService {
    @Override
    public List<ManBean> findAll() {
        this.flag=true;
       IManMapper MyMapper = session.getMapper(IManMapper.class);
        List<ManBean> list =MyMapper.findAll();
        this.commitAndClose();
        return list;
    }

    @Override
    public void add(ManBean man, List<CarBean> carList) {
        IManMapper MyMapper = session.getMapper(IManMapper.class);
        //完成公民的添加
        MyMapper.addMan(man);
        //添加汽车集合，所有汽车都是新加公民的id
        MyMapper.addCar(carList,man.getId());
        this.commitAndClose();
    }

    @Override
    public void delSetNull(int id) {
        IManMapper MyMapper = session.getMapper(IManMapper.class);
        MyMapper.delSetNull(id);
        this.commitAndClose();
    }

    @Override
    public ManBean findByIdIncludeCar(int id) {
        IManMapper MyMapper = session.getMapper(IManMapper.class);
        ManBean man = MyMapper.findByIdIncludeCar(id);

        this.commitAndClose();

        return man;
    }
    public static void main(String[] args){
        IManService service = new ManServiceImpl();
//        System.out.println(service.findAll());
//        service.add(new ManBean("刘大","18888888888"),
//                Arrays.asList(new CarBean("大运",40000),
//                        new CarBean("大运",60000),
//                        new CarBean("法拉利",100000)));
//        service.delSetNull(6);
        System.out.println(service.findByIdIncludeCar(1));
    }
}
